/**
 * 添加
 * 提交form表单操作
 */
$("#button-submit-add").click(function(){
	save_url = SCOPE.save_url;
	jump_url = SCOPE.jump_url;
    var data = $("#submit-form").serializeArray();
    console.log(data,1)
    postData = {};
    $(data).each(function(i){
    	if(this.name == 'style'){
    		postData[this.name] = (postData[this.name] == undefined ? '' : postData[this.name]) + ','+this.value
    	}else{
    		postData[this.name] = this.value;
    	}
         
     });
    console.log()
    $.ajax({
		type: 'POST',
		url: save_url,
		data:postData,
		dataType: 'json',
		success: function(res){

			if(res.code == 1) {
            	//成功
	            layer.msg('添加成功!',{icon: 1,time:1000});
	            setTimeout(function(){
	      			 window.location.href=jump_url;
	    		 }, 1000);
	               
		        }else{
		            // 失败
		            layer.msg(res.msg);
	        }
					
		},
		error:function(res) {

			layer.msg(res.msg);
		},
	});		
    
});

/**
 * 编辑
 * 提交form表单操作
 */
$("#button-submit-edit").click(function(){
	update_url = SCOPE.update_url;
	jump_url = SCOPE.jump_url;
    var data = $("#submit-form").serializeArray();
    postData = {};
    $(data).each(function(i){
         if(this.name == 'style'){
    		postData[this.name] = (postData[this.name] == undefined ? '' : postData[this.name]) + ','+this.value
    	}else{
    		postData[this.name] = this.value;
    	}
    });
    
    $.ajax({
		type: 'POST',
		url: update_url,
		data:postData,
		dataType: 'json',
		success: function(res){
			if(res.code == 1) {//成功
            	layer.msg('更新成功', {icon: 1,time:1000});
            	setTimeout(function(){
      			 	window.location.href=jump_url;
    		 	}, 1000);
               
	        }else{// 失败
	            layer.msg(res.msg);
	        }
					
		},
		error:function(res) {
			layer.msg(res.msg);
		},
	});		
     
});

/*
 * 上传图片
 */
$('#file').change(function(){
	upload_url = SCOPE.upload_url;
	var formData = new FormData();
	formData.append("image", document.getElementById("file").files[0]);
	$.ajax({
		url: upload_url,
		type: 'post',
		data: formData,
		headers: {
            'X-CSRF-TOKEN': $('meta[name="token"]').attr('token'),
        },
		cache: false,
		/**
		*必须false才会自动加上正确的Content-Type
		*/
		contentType: false,
		/**
		* 必须false才会避开jQuery对 formdata 的默认处理
		* XMLHttpRequest会对 formdata 进行正确的处理
		*/
		processData: false,
		success: function (res) {
		    $('#img_src').attr('src',res.data)
		    $('#img_src').show()
		   	$('#img_url').val(res.data)
		}
	})
});


//删除
function admin_del(obj,id,delete_url,jump_url){
	layer.confirm('确认要删除吗？',function(index){
		$.ajax({
			type: 'POST',
			url: delete_url,
			data:{id:id,_token: $('meta[name="token"]').attr('token')},
			dataType: 'json',
			success: function(data){
				layer.msg('已删除!',{icon:1,time:1000});
				layer.close(index);
				window.location.href=jump_url
			},
			error:function(data) {
				console.log(data.msg);
				layer.close(index);
			},
		});		
	});
}



//上下架
$('.updownOperate').click(function(){
	var current_page = $("table[id='table']").attr('current_page')
	var id = $(this).attr('data-id');
	var updown = $(this).attr('data-updown');
		
	updown_url = SCOPE.updown_url
	jump_url = SCOPE.jump_url;
	$.ajax({
		type: 'POST',
		url: updown_url,
		data:{id:id,updown:updown,_token: $('meta[name="token"]').attr('token')},
		dataType: 'json',
		success: function(res){				
			if(res.code == 1){
				window.location.href=jump_url+'?page='+current_page
			}else{
				layer.msg(res.msg);
			}
		},
		error:function(res) {
			layer.msg(res.msg);
		},
	});			
});


//更改状态
$('.statusOperate').click(function(){
	var id = $(this).attr('data-id');
	var status = $(this).attr('data-status');
	status_url = SCOPE.status_url
	jump_url = SCOPE.jump_url;

	if(status == 0){//提示
		layer.confirm('确认审核通过吗？', {
		  btn: ['成功','失败'] //按钮
		}, function(){
			
		  check(2,id);
		}, function(){
			
		  check(1,id);
		});	
		 
	}else{
		check(status,id);
	}
	
});

function check(status,id){
	var current_page = $("table[id='table']").attr('current_page')
	$.ajax({
		type: 'POST',
		url: status_url,
		data:{id:id,status:status,_token: $('meta[name="token"]').attr('token')},
		dataType: 'json',
		success: function(res){				
			if(res.code == 1){
				window.location.href=jump_url+'?page='+current_page
			}else{
				layer.msg(res.msg);
			}
		},
		error:function(res) {
			layer.msg(res.msg);
		},
	});		
}
	